package cn.itsource.pethome.service.impl;

import cn.itsource.pethome.domain.User;
import cn.itsource.pethome.mapper.UserMapper;
import cn.itsource.pethome.query.UserQuery;
import cn.itsource.pethome.result.JSONResult;
import cn.itsource.pethome.result.PageList;
import cn.itsource.pethome.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @description: service实现类
 * @auth: wujiangbo
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper UserMapper;

    @Override
    public User selectById(Long id) {
        return UserMapper.selectByPrimaryKey(id);
    }

    @Override
    public JSONResult pagelist(UserQuery query) {
        Example example = new Example(User.class);
        //封装查询条件


        //设置分页信息
        PageHelper.startPage(query.getCurrent(), query.getSize());

        //查询
        List<User> users = UserMapper.queryData(query);

        //返回结果集（使用PageInfo对象进行封装）
        return JSONResult.success(new PageInfo<>(users));
    }

    @Override
    public List<User> list() {
        List<User> list = UserMapper.selectAll();
        return list;
    }

    @Override
    public JSONResult saveOrUpdate(User dept) {
        if(dept.getId() != null){
            //编辑操作
            UserMapper.updateByPrimaryKey(dept);
        }else{
            //新增操作
            UserMapper.insert(dept);
        }
        return JSONResult.success();
    }

    @Override
    public JSONResult deleteBatch(UserQuery UserQuery) {
        for (Long id : UserQuery.getIds()) {
            UserMapper.deleteByPrimaryKey(id);
        }

        return  JSONResult.success();
    }
    //根据部门id删除部门信息,所有子节点所有字节点全部要删除
    @Override
    public JSONResult delete(Long id) {
        UserMapper.deleteByPrimaryKey(id);
        return JSONResult.success();
    }
}